﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKPatternSign.StairsRoomSection.ElementPacket;

namespace YJKPatternSign.MaterialManager
{
	internal class MateriaDataManger
	{
		public MateriaDataManger(Autodesk.Revit.DB.Document doc)
		{
			this.SavePath = this.GetSavePath();
			this.document = doc;
		}

		public bool WriteData()
		{
			XmlDocument xmlDocument = new XmlDocument();
			if (!File.Exists(this.SavePath))
			{
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement("Root");
				xmlDocument.AppendChild(newChild2);
			}
			else
			{
				xmlDocument.Load(this.SavePath);
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Root");
			if (xmlNode == null)
			{
				return false;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			for (int i = 0; i < this.Datas.Count; i++)
			{
				this.Datas[i].WriteData(xmlDocument, xmlNode, childNodes);
			}
			xmlDocument.Save(this.SavePath);
			return true;
		}

		public bool ReadData()
		{
			if (!File.Exists(this.SavePath))
			{
				return false;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.SavePath);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode("Root").ChildNodes;
			for (int i = 0; i < this.Datas.Count; i++)
			{
				this.Datas[i].ReadData(childNodes);
			}
			return true;
		}

		public bool GetAllMaterial(List<BaseLinkDocumentResultBean> elements)
		{
			List<string> elementAllMateria = this.GetElementAllMateria(elements);
			List<string> list = new List<string>();
			for (int i = 0; i < elementAllMateria.Count; i++)
			{
				if (list.IndexOf(elementAllMateria[i]) == -1)
				{
					MateriaData item = new MateriaData(elementAllMateria[i], this.document);
					list.Add(elementAllMateria[i]);
					this.Datas.Add(item);
				}
			}
			this.ReadData();
			return true;
		}

		public List<string> GetElementAllMateria(List<BaseLinkDocumentResultBean> elements)
		{
			List<string> list = new List<string>();
			List<ElementObject> list2 = new List<ElementObject>();
			for (int i = 0; i < elements.Count; i++)
			{
				BaseLinkDocumentResultBean baseLinkDocumentResultBean = elements[i];
				if (baseLinkDocumentResultBean.UseElement is Stairs)
				{
					list2.Add(new StairsObject(baseLinkDocumentResultBean, null));
				}
				else if (baseLinkDocumentResultBean.UseElement is Wall)
				{
					list2.Add(new WallObject(baseLinkDocumentResultBean, null));
				}
				else if (baseLinkDocumentResultBean.UseElement is Floor)
				{
					list2.Add(new FloorObject(baseLinkDocumentResultBean, null));
				}
				else if (baseLinkDocumentResultBean.UseElement is FamilyInstance && baseLinkDocumentResultBean.UseElement.Category.Id.IntegerValue == -2001320)
				{
					list2.Add(new StructframeObject(baseLinkDocumentResultBean, null));
				}
				else if (baseLinkDocumentResultBean.UseElement is FamilyInstance && baseLinkDocumentResultBean.UseElement.Category.Id.IntegerValue == -2001330)
				{
					list2.Add(new ColumnObject(baseLinkDocumentResultBean, null));
				}
				else if (baseLinkDocumentResultBean.UseElement.Category.Id.IntegerValue == -2000035)
				{
					list2.Add(new RoofObject(baseLinkDocumentResultBean, null));
				}
			}
			List<ElementObject> list3 = new List<ElementObject>();
			for (int j = 0; j < list2.Count; j++)
			{
				list2[j].LstChildObj = list2[j].DividChild();
				if (list2[j].LstChildObj.Count == 0)
				{
					list3.Add(list2[j]);
				}
				else
				{
					list3.AddRange(list2[j].LstChildObj);
				}
			}
			using (List<ElementObject>.Enumerator enumerator = list3.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ElementObject oneObject = enumerator.Current;
					oneObject.GetMaterial();
					if (list.FindIndex((string e) => e.Equals(oneObject.m_strMaterial)) == -1)
					{
						list.Add(oneObject.m_strMaterial);
					}
				}
			}
			return list;
		}

		public void SetFillName(string name, int row, int col)
		{
			for (int i = 0; i < this.Datas.Count; i++)
			{
				for (int j = 0; j < this.Datas[i].ScalesData.Count; j++)
				{
					if (this.Datas[i].ScalesData[j].rowInGrid == row && this.Datas[i].ScalesData[j].colInGrid == col)
					{
						this.Datas[i].ScalesData[j].NameModify = name;
					}
				}
			}
		}

		public void SetFillColor(System.Drawing.Color color, int row, int col)
		{
			for (int i = 0; i < this.Datas.Count; i++)
			{
				for (int j = 0; j < this.Datas[i].ScalesData.Count; j++)
				{
					if (this.Datas[i].ScalesData[j].rowInGrid == row && this.Datas[i].ScalesData[j].colInGrid == col - 1)
					{
						this.Datas[i].ScalesData[j].ColorModify = new Autodesk.Revit.DB.Color(color.R, color.G, color.B);
					}
				}
			}
		}

		public FillPattern GetFill(int row, int col)
		{
			FillPattern result = null;
			for (int i = 0; i < this.Datas.Count; i++)
			{
				for (int j = 0; j < this.Datas[i].ScalesData.Count; j++)
				{
					if (this.Datas[i].ScalesData[j].rowInGrid == row && this.Datas[i].ScalesData[j].colInGrid == col)
					{
						result = this.Datas[i].ScalesData[j].GetFillPattern();
					}
				}
			}
			return result;
		}

        public Autodesk.Revit.DB.Color GetFillColor(int row, int col)
		{
            Autodesk.Revit.DB.Color result = null;
			for (int i = 0; i < this.Datas.Count; i++)
			{
				for (int j = 0; j < this.Datas[i].ScalesData.Count; j++)
				{
					if (this.Datas[i].ScalesData[j].rowInGrid == row && this.Datas[i].ScalesData[j].colInGrid == col - 1)
					{
						result = this.Datas[i].ScalesData[j].ColorModify;
					}
				}
			}
			return result;
		}

		public FillPattern GetFillPattern(string matrialName, int scale)
		{
			FillPattern result = null;
			for (int i = 0; i < this.Datas.Count; i++)
			{
				if (!(this.Datas[i].Name != matrialName) && this.Datas[i].ScalesData.Count == 2)
				{
					if (scale <= 100)
					{
						result = this.Datas[i].ScalesData[0].GetFillPattern();
					}
					else
					{
						result = this.Datas[i].ScalesData[1].GetFillPattern();
					}
				}
			}
			return result;
		}

		public string GetFillPatternName(string matrialName, int scale)
		{
			FillPatternElement fillPatternElement = null;
			int i = 0;
			while (i < this.Datas.Count)
			{
				if (!(this.Datas[i].Name != matrialName) && this.Datas[i].ScalesData.Count == 2)
				{
					if (scale < 100)
					{
						fillPatternElement = this.Datas[i].ScalesData[0].GetFillPatternElement();
						break;
					}
					fillPatternElement = this.Datas[i].ScalesData[1].GetFillPatternElement();
					break;
				}
				else
				{
					i++;
				}
			}
			if (fillPatternElement != null)
			{
				return fillPatternElement.Name;
			}
			return string.Empty;
		}

        public Autodesk.Revit.DB.Color GetColor(string matrialName, double scale)
		{
            Autodesk.Revit.DB.Color result = null;
			int i = 0;
			while (i < this.Datas.Count)
			{
				if (!(this.Datas[i].Name != matrialName) && this.Datas[i].ScalesData.Count == 2)
				{
					if (scale < 100.0)
					{
						result = this.Datas[i].ScalesData[0].ColorModify;
						break;
					}
					result = this.Datas[i].ScalesData[1].ColorModify;
					break;
				}
				else
				{
					i++;
				}
			}
			return result;
		}

		private string GetSavePath()
		{
			return AssistFunc.GetAppRootPath() + "\\Data\\StairsSecFillPatternConfig.xml";
		}

		public Document document;

		public List<MateriaData> Datas = new List<MateriaData>();

		private string SavePath = "";
	}
}
